package com.zxm.edu.controller;

import cn.hutool.json.JSONObject;
import com.zxm.edu.annotation.OptLog;
import com.zxm.edu.dto.CourseDTO;
import com.zxm.edu.entity.Course;
import com.zxm.edu.service.CourseService;
import com.zxm.edu.vo.ConditionVO;
import com.zxm.edu.vo.CourseVO;
import com.zxm.edu.vo.PageResult;
import com.zxm.edu.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.zxm.edu.constant.OptTypeConst.REMOVE;
import static com.zxm.edu.constant.OptTypeConst.SAVE_OR_UPDATE;

/**
 * 课程控制器
 *
 * @author silencezheng
 * @date 2022/09/04
 */
@Api(tags = "课程模块")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 查看课程列表
     *
     * @param condition 条件
     * @return {@link Result < CourseDTO >} 课程列表
     */
    @ApiOperation(value = "查看课程列表")
    @GetMapping("/courses/list")
    public Result<PageResult<CourseDTO>> listCourses(ConditionVO condition) {
        return Result.ok(courseService.listCourses(condition));
    }
    /**
     * 查看课程详情
     *
     * @param id 条件
     * @return {@link Result<Course>} 课程详情
     */
    @ApiOperation(value = "查看课程详情")
    @GetMapping("/courses/listDetails")
    public Result<Course> listCourseById(Integer id) {
        return Result.ok(courseService.listCourseById(id));
    }

    /**
     * 查看后台课程列表
     *
     * @param condition 条件
     * @return {@link Result<CourseDTO>} 课程列表
     */
    @ApiOperation(value = "查看后台课程列表")
    @GetMapping("/admin/courses/list")
    public Result<PageResult<CourseDTO>> listBackCourses(ConditionVO condition) {
        return Result.ok(courseService.listCourses(condition));
    }

    /**
     * 添加或修改课程
     *
     * @param courseVO 课程信息
     * @return {@link Result<>}
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改课程")
    @PostMapping("/admin/course")
    public Result<?> saveOrUpdateCourse(@Valid @RequestBody CourseVO courseVO) {
        courseService.saveOrUpdateCourse(courseVO);
        return Result.ok();
    }

    /**
     * 删除课程
     *
     * @param courseIdList 课程id列表
     * @return {@link Result<>}
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除课程")
    @DeleteMapping("/admin/courses")
    public Result<?> deleteCourses(@RequestBody List<Integer> courseIdList) {
        courseService.deleteCourses(courseIdList);
        return Result.ok();
    }
}
